<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\services\ProxyIP2;

/**
 * 示例接口
 */
class Ip extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 无需登录的接口
     *
     */
    public function getPorxyIp()
    {
        $validate = new \think\Validate([
            'type'    =>'require',
        ]);
        $params = $this->request->get();
        if(!$validate->check($params)){
            $this->error($validate->getError());
        }
        $service = new ProxyIP2();
        $array = $service->getTask($params);
        if(!$array){
            $this->error('fail', []);
        }

        $this->success('succ', $array);
    }


    //创建zdaye---redis链表--120长度
    public function zdaye_redis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(2);

        if($redis->lLen('ProxyIpList') < 120){ //队列维护长度在 120
            $service = new ProxyIP2();
            $res = $service->get_zdaye_redis();
            $data = json_decode($res,true);
//            dump($data['data']['proxy_list']);die;
            if($data['code'] == 10001){
                $res = $data['data']['proxy_list'];
                $redis->incrBy('zdaye_count',10);
                foreach ($res as $item){
                    $item['time'] = time()+180;//设定过期时间为3分钟
                    $redis->rpush('ProxyIpList',json_encode($item));
                }
            }else{
                echo $res;die;
            }
            echo  'success';
        }else{
            echo  '----队列长度大于120，不用更新--------';
        }
    }

    //创建zdaye---redis链表--120长度
    public function shenlong_redis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(2);
        if($redis->lLen('ProxyIpList') <= 36){ //队列维护长度在 16
            $service = new ProxyIP2();
            $res = $service->get_shenlong_redis();
            $data = json_decode($res,true);
            if($data['code'] == 200){
                $res = $data['data'];
                foreach ($res as $item){
                    $item['time'] = time()+75;//设定过期时间为1分钟
                    $redis->rpush('ProxyIpList',json_encode($item));
                }
            }else{
                echo $res;die;
            }
            echo  'success';
        }else{
            echo  '----队列长度大于120，不用更新--------';
        }

    }



}
